#ifndef CODE_SYNTAXHIGHLIGHT_H
#define CODE_SYNTAXHIGHLIGHT_H

#include <QSyntaxHighlighter>
#include <QTextDocument>
#include <QVector>
#include <QTextCharFormat>
#include <QRegularExpression>

class CodeSyntaxHighLight : public QSyntaxHighlighter
{
    Q_OBJECT
public:
    CodeSyntaxHighLight(QTextDocument* parent);

protected:
    virtual void highlightBlock(const QString& text) override;

private:
    struct _HightRule {
        QRegularExpression pattern;  //匹配格式
        QTextCharFormat format;      //样式
    };

    QVector<_HightRule> _rules;
    QTextCharFormat _keywordFormat;
    QTextCharFormat _classFormat;
    QTextCharFormat _numberFormat;
    QTextCharFormat _functionFormat;
    QTextCharFormat _quotationFormat;
    QTextCharFormat _singleLineCommentFormat;
    QTextCharFormat _multiLineCommentFormat;
    QRegularExpression _multiStartExp;
    QRegularExpression _multiEndExp;

    void initRules();
    void initKeywordsRule();
    void initClassRule();
    void initNumberRule();
    void initFunctionRule();
    void initQuotationRule();
    void initSingleLineCommentRule();
    void initMultiLineCommentRule();
    void matchMultiLineComment(const QString& text);
};

#endif // CODE_SYNTAXHIGHLIGHT_H
